package guiTesting;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.OperationFailedException;
import com.atlassian.crowd.model.user.User;



public class HandCodedGUI extends JPanel implements TableModelListener {
	private RetrieveUserInfo userInfo;
	private List<User> searchResults;
	
	Vector rowData = new Vector();
	DefaultTableModel dModel ; 

	JFrame appFrame; 
	JLabel sTerm;
	JButton sButton; 
	JTextField sTextField;
	//Container allComp; 
	JPanel cPanel; 
	
	JPanel allComp; 
	JButton cancelButton;
	GridBagConstraints mainL ;

	JScrollPane sTable; 
	JTable eTable; 

	static Vector columnNamesV ;
	static String[] columnNames  = { "First Name ", "Last Name","Email" , "Display Name", "Is Active"};


	final static boolean shouldFill = true;
	final static boolean shouldWeightX = true;
	final static boolean RIGHT_TO_LEFT = false;



	public HandCodedGUI(){
		createGUI();
		createEvents();

	}
	
	
	public void createGUI() {

		//disabling the JFrame
		//appFrame = new JFrame("Hand Coded GUI let hope it works");
		//cPane = appFrame.getContentPane();
		allComp = new JPanel(); 
		//allComp.add(cPane);
		
		allComp.setLayout(new GridBagLayout());
		componentsLayout();
		
		//old container 
		
		//appFrame.setSize(500,300);
		//appFrame.setResizable(true);
		//appFrame.setVisible(true);
		//appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//allComp.add(cPanel);
		
		 mainL = new GridBagConstraints();


		if (shouldFill) {
			//natural height, maximum width
			mainL.fill = GridBagConstraints.HORIZONTAL;
		}
				
		mainL.fill = GridBagConstraints.HORIZONTAL;
		mainL.weightx = 0.5;
		mainL.gridx = 0;
		mainL.gridy = 1;
		mainL.ipadx = 2;
		mainL.ipady = 2;
		mainL.anchor = GridBagConstraints.NORTH;
		allComp.add(cPanel, mainL);
	   

	}

	
	
	public void addTable(){
		
		dModel = new DefaultTableModel(rowData,columnNamesV){
			public boolean isCellEditable(int row, int col) {
				return false;
			}
			
		}; 
		
		eTable = new JTable(dModel);
		
		eTable.getModel().addTableModelListener(this);

		eTable.setPreferredScrollableViewportSize(new Dimension(600, 400));
		sTable = new JScrollPane(eTable);
		//sTable.setPreferredSize(new Dimension(400, 200));
		sTable.setBorder(BorderFactory.createLineBorder(Color.red));
		
		
		//GridBagConstraints mainL = new GridBagConstraints();
		if (shouldFill) {
			//natural height, maximum width
			mainL.fill = GridBagConstraints.HORIZONTAL;
		}
		
		mainL.fill = GridBagConstraints.BOTH;
		mainL.weightx = 0.5;
		mainL.gridx = 0;
		mainL.gridy = 2;
		mainL.ipadx = 2;
		mainL.ipady = 2;
		mainL.anchor = GridBagConstraints.SOUTH;
		allComp.add(sTable, mainL);
		
		
	}

	
	public void componentsLayout(){
		GridBagConstraints sInputLayout = new GridBagConstraints();
		cPanel = new JPanel(new GridBagLayout());
		cPanel.setBorder(BorderFactory.createLineBorder(Color.green)); 

		if (shouldFill) {
			//natural height, maximum width
			sInputLayout.fill = GridBagConstraints.HORIZONTAL;
		}

		sTerm = new JLabel("Enter Search Term");
		sInputLayout.fill = GridBagConstraints.HORIZONTAL;
		sInputLayout.weightx = 0.5;
		sInputLayout.gridx = 1;
		sInputLayout.gridy = 0;
		sInputLayout.ipadx = 2;
		sInputLayout.ipady = 2;
		cPanel.add(sTerm, sInputLayout);

		sTextField = new JTextField(20);
		Dimension comDim = new Dimension (50, 30); 
		sTextField.setPreferredSize(comDim);
		sInputLayout.fill = GridBagConstraints.HORIZONTAL;
		sInputLayout.weightx = 0.5;
		sInputLayout.gridx = 2;
		sInputLayout.gridy = 0;
		sInputLayout.ipadx = 2;
		sInputLayout.ipady = 2;
		cPanel.add(sTextField, sInputLayout);


		sButton = new JButton("Search Users");
		sButton.setPreferredSize(comDim);
		sInputLayout.fill = GridBagConstraints.HORIZONTAL;
		sInputLayout.weightx = 0.5;
		sInputLayout.gridx = 1;
		sInputLayout.gridy = 2;
		sInputLayout.ipadx = 2;
		sInputLayout.ipady = 2;
		cPanel.add(sButton, sInputLayout);

		cancelButton = new JButton("Cancel Search");
		cancelButton.setPreferredSize(comDim);
		sInputLayout.fill = GridBagConstraints.HORIZONTAL;
		sInputLayout.weightx = 0.5;
		sInputLayout.gridx = 2;
		sInputLayout.gridy = 2;
		sInputLayout.ipadx = 2;
		sInputLayout.ipady = 2;
		cPanel.add(cancelButton, sInputLayout);

	}

	public void tableChanged(TableModelEvent e){
		int row = e.getFirstRow();
		int column = e.getColumn(); 
		TableModel model = (TableModel) e.getSource(); 
		String columnName = model.getColumnName(column);
		Object data = model.getValueAt(row, column);
		
		Vector c = new Vector(); 
		c.add(columnName); 
		Vector r = new Vector(); 
		r.add(data); 
		dModel = new DefaultTableModel(r, c);
		
	}
	private void createEvents(){
		sButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (sTextField.getText()!=null){
					try {
						//System.out.println(textFieldSearchTerm.getText());
						userInfo = new RetrieveUserInfo();
						searchResults = userInfo.searchUsers(sTextField.getText());
						
						//Vector rowData = new Vector();
						if (searchResults.size()==0){
							JOptionPane.showMessageDialog(null, "No User Found", "Info", JOptionPane.INFORMATION_MESSAGE);
						}
						for (int i=0; i<searchResults.size(); i++){
							User uz =  searchResults.get(i);
							Vector colData = new Vector(Arrays.asList( uz.getFirstName(), uz.getLastName(),uz.getEmailAddress(), uz.getDisplayName(),uz.isActive()));
							
							
							//System.out.println ( rowData.get(2)) ; 
						
							System.out.println(uz.getEmailAddress());
						
							rowData.add(colData);
							columnNamesV = new Vector(Arrays.asList(columnNames));
							
							
							//System.out.println( columnNamesV.get(1));
						}
						
					//	System.out.println(rowData); 
						
						addTable();
						
					} catch (OperationFailedException e1) {
						JOptionPane.showMessageDialog(null, "Search Failed", "Error", JOptionPane.ERROR_MESSAGE);
						e1.printStackTrace();
					} catch (InvalidAuthenticationException e2) {
						JOptionPane.showMessageDialog(null, "APP Authentication Failed", "Error", JOptionPane.ERROR_MESSAGE);
						e2.printStackTrace();
					} catch (ApplicationPermissionException e3) {
						e3.printStackTrace();
					}

				} else {
					JOptionPane.showMessageDialog(null, "Please enter search term", "Info", JOptionPane.INFORMATION_MESSAGE);

				}

			}


		});

		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				userInfo.getClient().shutdown();
				System.out.println("Search Cancel Button Pressed");
				
			}
			
		});
		

	}

	public JPanel getAllComp(){
		return allComp;
	}



	public static void main(String args[]){
	//	new HandCodedGUI(); 
	}

}
